﻿using System.Windows.Input;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;

namespace TestExtensions
{
    public static class StartMenu
    {
        public static void Run(string commandText, params string[] args)
        {
            commandText = string.Join(" ", new string[] { commandText, string.Join(" ", args) });

            // Press the "Start" key:
            Keyboard.SendKeys("{LWin}", ModifierKeys.None);

            // Click 'Run...' menu item:
            new UISearch()
                .Where(WinWindow.PropertyNames.Name).Equals("Start Menu")
                .Where(WinWindow.PropertyNames.ClassName).Equals("DV2ControlHost").Find<WinWindow>()
                .Where(WinWindow.PropertyNames.ControlId).Equals("3").Find<WinWindow>().Find<WinClient>()
                .Where(WinMenuItem.PropertyNames.Name).Equals("Run...").Find<WinMenuItem>()
                .Click();

            // Select {commandText} in 'Open:' combo box:
            new UISearch()
                .Where(WinWindow.PropertyNames.Name).Equals("Run")
                .Where(WinWindow.PropertyNames.ClassName).Equals("#32770").Find<WinWindow>()
                .Where(WinWindow.PropertyNames.ControlId).Equals("12298").Find<WinWindow>()
                .Where(WinComboBox.PropertyNames.Name).Equals("Open:").Find<WinComboBox>()
                .SetEditableItem(commandText)
                .SendKeys("{Enter}", ModifierKeys.None);
        }
    }
}